<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:q="livcos.org/ns/query"
	xmlns:db="livcos.org/ns/db"
	xmlns:ddl="livcos.org/ns/db/ddl"
	xmlns="http://www.w3.org/1999/XSL/Format"
	xmlns:f="livcos.org/ns/xslt/function"
	xmlns:v="livcos.org/ns/view"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	exclude-result-prefixes="q db ddl f v xs">
	
	<xsl:import href="../../../../base.xsl"/>
  
	<xsl:attribute-set name="cell">
		<xsl:attribute name="padding-top">1pt</xsl:attribute>
		<xsl:attribute name="padding-right">1pt</xsl:attribute>
		<xsl:attribute name="padding-bottom">0pt</xsl:attribute>
		<xsl:attribute name="padding-left">1pt</xsl:attribute>
		<xsl:attribute name="margin-left">0pt</xsl:attribute>
	</xsl:attribute-set>
	
	<xsl:template match="q:sql">
		<xsl:variable name="res" select="f:db-query(., @connection, true(), true())/*"/>
		<table table-layout="fixed" width="100%" margin-left="0.6cm" font-size="5pt">
			<xsl:apply-templates select="$res/ddl:meta/ddl:column"/>
			<table-header font-weight="bold" background-color="#EEEEEE">
				<table-row>
					<xsl:apply-templates select="$res/ddl:meta/ddl:column" mode="header"/>
				</table-row>
			</table-header>
			<table-body margin-left="0cm">
				<xsl:apply-templates select="$res//db:*[db:* and not(db:*/db:*)]" mode="row"/>
			</table-body>
		</table>
	</xsl:template>
	
	<xsl:template match="ddl:column">
		<xsl:variable name="w" select="if (@size > 40) then 40 else @size"/>
		<table-column column-width="proportional-column-width({$w})"/>
	</xsl:template>
	
	<xsl:template match="ddl:column" mode="header">
		<table-cell border-style="solid" border-width="thin" xsl:use-attribute-sets="cell">
			<block text-align="center">
				<xsl:value-of select="@label" />
			</block>
		</table-cell>
	</xsl:template>
	
	<xsl:template match="db:*" mode="row">
		<table-row>
			<xsl:apply-templates select="ancestor-or-self::db:*/db:*[not(db:*)]"/>
		</table-row>
	</xsl:template>
	
	<xsl:template match="db:*">
		<table-cell border-style="solid" border-width="thin" xsl:use-attribute-sets="cell">
			<block>
				<xsl:variable name="p" select="position()"/>
				<xsl:variable name="t" select="/*/ddl:meta/ddl:column[position() = $p]/@type"/>
				<xsl:if test="($t = 'int') or ($t = 'smallint')">
					<xsl:attribute name="text-align">right</xsl:attribute>
				</xsl:if>
				<xsl:if test="$t = 'bit'">
					<xsl:attribute name="text-align">center</xsl:attribute>
				</xsl:if>
				<xsl:value-of select="."/>
			</block>
		</table-cell>
	</xsl:template>
	
</xsl:stylesheet>
